<?php

namespace pmvc\template\smarty;

use pmvc\template\Template;
use pmvc\io\Stream;

use Smarty;

/**
 * Smarty implementation of the {@link Template} interface.
 *
 */
class SmartyTemplate
	implements Template {

	private $smarty 	= NULL;
	private $template	= NULL;

	/**
	 * Creates the {@link SmartyTemplate}.
	 * @param Smarty $smarty
	 * @param string $template
	 */
	public function __construct(Smarty &$smarty, $template) {
		$this->smarty 	= $smarty;
		$this->template	= $template;
	}

	/**
	 * Returns the configured smarty object.
	 */
	public function getSmarty() {
		return $this->smarty;
	}

	/**
	 * Returns a template as a string with the
	 * given variables.
	 * @param array $variables
	 * @return string
	 */
	public function renderAsString(array $variables) {
		SmartyUtil::assignTemplateVars($this->smarty, $variables);
		$ret = SmartyUtil::render($this->smarty, $this->template);
		SmartyUtil::clearTemplateVars($this->smarty, array_keys($variables));
		return $ret;
	}

	/**
	 * Returns a template to the given {@link Stream}
	 * with the given variables.
	 * @param array $variables
	 * @param Stream $stream
	 */
	public function render(array $variables, Stream $stream) {
		$stream->write($this->renderAsString($variables));
	}

}

?>